/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Business;

import Business.Mail.MailUtility;
import Business.File.FacadeStampa;
import DataAccess.ClienteFornitoreHtml;
import Business.File.FileFacade;
import Business.File.Percorsi;
import DataAccess.ManagerDatabase;
import DataAccess.entity.Criteria;
import DataAccess.entity.Property;
import Presentation.FrontController;
import Presentation.Model.ListTableModel;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.MessagingException;
/**
 *
 * @author Domenico
 */

public class BusinessDelegate {


    public static void inizializza() {
        FrontController.inizializza();
    }
    public static void stampaFattura() {
        FacadeStampa facadeStampa = new FacadeStampa();
        facadeStampa.stampaFattura();
    }

    public static void stampaOrdineFornitore() {
        FacadeStampa facadeStampa = new FacadeStampa();
        facadeStampa.stampaOrdineFornitore();
    }
    public static void stampaClienteFornitore() {
        FacadeStampa facadeStampa = new FacadeStampa();
        facadeStampa.stampaClienteFornitore();
    }
    public static void inviaMail() {
        MailUtility mail = new MailUtility();
        try {
            mail.sendMail("colletta.domenico@gmail.com", "colletta.domenico@gmail.com", "PROVAJAVA", "testo testoS");
        } catch (MessagingException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public static ListTableModel ottieniListaClienti() {
        try {
            ManagerDatabase.connettiDb();
            ResultSet rs = ManagerDatabase.ricercaListaClienti();
            ListTableModel model = ListTableModel.createModelFromResultSet(rs);
            ManagerDatabase.disconnettiDb();
            return model;
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
            return null;
    }
    public static ListTableModel ottieniListaClienti(ArrayList<Criteria> listaCriteri) {
        try {
            ManagerDatabase.connettiDb();
            ResultSet rs = ManagerDatabase.ricercaListaClienti(listaCriteri);
            ListTableModel model = ListTableModel.createModelFromResultSet(rs);
            ManagerDatabase.disconnettiDb();
            return model;
        } catch (SQLException ex) {
            return null;
        }
    }
    
    
    public static void ottieniHTMLClienteFornitore(int indice) {
        ManagerDatabase.connettiDb();
        ResultSet rs = ManagerDatabase.ricercaClienteFornitore(indice);
        ClienteFornitoreHtml cfhtml = new ClienteFornitoreHtml(rs);
        cfhtml.inserisciValori();
        ManagerDatabase.disconnettiDb();
    }

    public static String ottieniTestoFile(String percorso) {
        FileFacade ff = new FileFacade();
        return ff.ottieniTestoFile(percorso);
    }

    public static void creaDocumentoHTML(String percorsoFile, String nomeFile, String testo, ArrayList<String> listaFileCorrelati) {
        FileFacade ff = new FileFacade();
        ff.scriviFileDiTesto(percorsoFile, testo, nomeFile);
        for (int i = 0; i < listaFileCorrelati.size(); i++) {
            String percorso = listaFileCorrelati.get(i);
            ff.scriviFileDiTesto(Percorsi.PERCORSO_FILE_TEMPORANEI, ff.ottieniTestoFile(percorso), percorso.substring(percorso.lastIndexOf("\\") + 1));            
        }
    }

    public static ListTableModel ottieniListaFornitori() {
        try {
            ManagerDatabase.connettiDb();
            ResultSet rs = ManagerDatabase.ricercaListaFornitori();
            ListTableModel model = ListTableModel.createModelFromResultSet(rs);
            ManagerDatabase.disconnettiDb();
            return model;
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
            return null;
    }

    public static ListTableModel ottieniListaOrdiniclienti() {
        try {
            ManagerDatabase.connettiDb();
            ResultSet rs = ManagerDatabase.ricercaListaOrdiniclienti();
            ListTableModel model = ListTableModel.createModelFromResultSet(rs);
            ManagerDatabase.disconnettiDb();
            return model;
        } catch (SQLException ex) {
            Logger.getLogger(BusinessDelegate.class.getName()).log(Level.SEVERE, null, ex);
        }
            return null;
    }
    public static ArrayList<Property> ottieniListaProprietaCF (String clienteFornitore) {
        ArrayList<Property> lista = null;    
        
        ManagerDatabase.connettiDb();
        lista = ManagerDatabase.ottieniCampi(clienteFornitore);
        ManagerDatabase.disconnettiDb();
        return lista;            
    }

    public static Integer ottieniNuovo_nFattura() {
        ManagerDatabase.connettiDb();
        Integer i = ManagerDatabase.ottieniNuovo_nFattura();
        ManagerDatabase.disconnettiDb();
        return i;
    }

    
}
